#!/bin/sh

DAEMON="atftpd"
PIDFILE="/var/run/$DAEMON.pid"
ATFTPD_ARGS="--user root.root /tftpboot"

# shellcheck disable=SC1090
[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"

start() {
	printf 'Starting %s: ' "$DAEMON"
	# shellcheck disable=SC2086 # we need the word splitting
	start-stop-daemon -S -q -p "$PIDFILE" -x "/usr/sbin/$DAEMON" -- \
		--daemon --pidfile="$PIDFILE" $ATFTPD_ARGS
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

stop() {
	printf 'Stopping %s: ' "$DAEMON"
	start-stop-daemon -K -q -p "$PIDFILE" -x "/usr/sbin/$DAEMON"
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart|reload)
	stop
	start
	;;
*)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac
